import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ty_smart_watch/ble/ble_util.dart';
import 'package:ty_smart_watch/common/viewmodel.dart';
import 'package:ty_smart_watch/page/scandevices/controller/scan_device_data.dart';
import 'package:ty_smart_watch/page/scandevices/scan_device_item.dart';

import '../../common/images_url.dart';
import '../../utils/Log.dart';

class ScanDevicePage extends StatefulWidget {
  const ScanDevicePage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _ScanDevicePage();
  }
}

class _ScanDevicePage extends State<ScanDevicePage> {
  static final ScanDeviceController controller = Get.find();

  final List<ScanDeviceItem> deviceList = [
    const ScanDeviceItem(imgSrc: ICON_WATHC_STYLE_1, name: "智能手表", id: "蓝牙", device: null),
    const ScanDeviceItem(imgSrc: ICON_WATHC_STYLE_2, name: "智能手表", id: "蓝牙", device: null),
    const ScanDeviceItem(imgSrc: ICON_WATHC_STYLE_3, name: "智能手表", id: "蓝牙", device: null),
    const ScanDeviceItem(
        imgSrc: ICON_WATHC_STYLE_STUDENT_CARD, name: "智能学生卡", id: "蓝牙", device: null),
    const ScanDeviceItem(imgSrc: ICON_WATHC_STYLE_JUMP, name: "智能跳绳", id: "蓝牙", device: null),
    const ScanDeviceItem(imgSrc: ICON_WATHC_STYLE_TAG, name: "智能防丢器", id: "蓝牙", device: null),
  ];

  @override
  void initState() {
    super.initState();
    Log.d("初始化扫描页");
    BleUtil.startScanDevice();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("添加设备"),
        centerTitle: true,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios),
          onPressed: () {
            Get.back();
          },
        ),
        actions: [
          GestureDetector(
            onTap: () {
              Log.d("扫描二维码");
            },
            child: Image.asset(ICON_SCAN_CODE, width: 20, height: 20),
          ),
          const SizedBox(width: 10)
        ],
      ),
      body: Column(
        children: [
          // 自动搜索标题
          Container(
            margin: const EdgeInsets.only(left: 10),
            child: Row(
              children: [
                Image.asset(
                  SCAN_DEVICE_SCANING,
                  height: 12,
                  width: 12,
                ),
                const SizedBox(
                  width: 4,
                ),
                const Text("自动搜索附近设备")
              ],
            ),
          ),

          //搜索到的设备列表
          Obx(
            () => Flexible(
              child: Container(
                height: 140,
                // color: Colors.blueGrey,
                margin: const EdgeInsets.all(10),
                child: GridView.builder(
                  itemCount: controller.deviceList.length,
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3,
                    // 每行3个item
                    childAspectRatio: 0.8,
                    mainAxisSpacing: 10,
                    crossAxisSpacing: 10,
                    // mainAxisExtent: null // 设置主轴方向（垂直方向）的Item高度为动态测量
                  ),
                  itemBuilder: (BuildContext context, int index) {
                    return GestureDetector(
                      onTap: () {
                        Log.d("点击了： ${controller.deviceList[index].name}");
                        BleUtil.connectDevice(controller.deviceList[index], () {
                          ViewModel.getInitInfo(controller.deviceList[index].id);
                          // ViewModel.getInitInfo("36:ED:EC:E5:E4:A3");
                        });
                      },
                      child: ScanDeviceItem(
                        imgSrc: ICON_WATHC_STYLE_1,
                        name: controller.deviceList[index].name,
                        id: controller.deviceList[index].id,
                        device: controller.deviceList[index],
                      ),
                    );
                  },
                ),
              ),
            ),
          ),

          // 手动搜索标题
          Container(
            margin: const EdgeInsets.only(left: 10),
            child: const Row(
              children: [Text("手动添加")],
            ),
          ),
          const SizedBox(height: 10),
          //分割线
          Container(
            margin: const EdgeInsets.symmetric(horizontal: 10),
            height: 0.3,
            width: double.infinity, // 设置为屏幕宽度
            color: Colors.black,
          ),
          //手动添加图
          Expanded(
            child: Container(
              // color: Colors.blueGrey,
              margin: const EdgeInsets.all(10),
              child: GridView.builder(
                itemCount: deviceList.length,
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3,
                    // 每行3个item
                    childAspectRatio: 0.8,
                    mainAxisSpacing: 10,
                    crossAxisSpacing: 10,
                    mainAxisExtent: null // 设置主轴方向（垂直方向）的Item高度为动态测量
                    ),
                itemBuilder: (BuildContext context, int index) {
                  return GestureDetector(
                      onTap: () {
                        Log.d("点击了： ${deviceList[index].name}");
                      },
                      child: deviceList[index]);
                },
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    BleUtil.cancelScan();
    // BleUtil.clearDeviceList();
  }
}

//HH-029
//36:ED:EC:E5:E4:A3
//0000FFE0-E6B7-BBE7-BFBC-E7B2BEE5AF86
//0000FFE1-E6B7-BBE7-BFBC-E7B2BEE5AF86

//模块
//EC:B1:AC:00:04:4A
//0000fff0-0000-1000-8000-00805f9b34fb
//0xFFF0
//DiscoveredService(
// serviceId: 0000fff0-0000-1000-8000-00805f9b34fb,
// serviceInstanceId: 1,
// characteristicIds: [0000fff2-0000-1000-8000-00805f9b34fb, 0000fff1-0000-1000-8000-00805f9b34fb],
// characteristics:
// [DiscoveredCharacteristic(characteristicId: 0000fff2-0000-1000-8000-00805f9b34fb,
// serviceId: 0000fff0-0000-1000-8000-00805f9b34fb),
// DiscoveredCharacteristic(characteristicId: 0000fff1-0000-1000-8000-00805f9b34fb,
// serviceId: 0000fff0-0000-1000-8000-00805f9b34fb)],
// includedServices: [])
